package com.youbug.mall.ware_wms.controller;


import com.youbug.common.constant.WareConstant;
import com.youbug.common.to.PmsSkuInfoTo;
import com.youbug.common.vo.QueryVo;
import com.youbug.mall.ware_wms.entity.WmsPurchaseDetail;
import com.youbug.mall.ware_wms.feign.IWmsPmsFeignService;
import com.youbug.mall.ware_wms.service.IWmsPurchaseDetailService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import com.youbug.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author youbug
 * @since 2024-05-05
 */
@RestController
@RequestMapping("/wmsPurchaseDetail")
public class WmsPurchaseDetailController {
    @Autowired
    private IWmsPurchaseDetailService wmsPurchaseDetailService;
    @Autowired
    private IWmsPmsFeignService pmsFeignService;

    @PostMapping("/getWmsPurchaseDetailPage")
    public R<IPage<WmsPurchaseDetail>> getWmsPurchaseDetailPage(@RequestBody QueryVo<WmsPurchaseDetail> queryVo){

        IPage<WmsPurchaseDetail> wmsPurchaseDetails = wmsPurchaseDetailService.getWmsPurchaseDetailPage(queryVo);

        return R.ok(wmsPurchaseDetails);
    }

    @GetMapping("/getWmsPurchaseDetailById/{id}")
    public R<WmsPurchaseDetail> getWmsPurchaseDetailById(@PathVariable Integer id){

        WmsPurchaseDetail wmsPurchaseDetail = wmsPurchaseDetailService.getById(id);

        if(wmsPurchaseDetail!=null){
            PmsSkuInfoTo skuInfoTo = pmsFeignService.getSkuInfoById(wmsPurchaseDetail.getSkuId());
            wmsPurchaseDetail.setSkuNameTemp(skuInfoTo.getSkuName());
        }

        return R.ok(wmsPurchaseDetail);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody WmsPurchaseDetail wmsPurchaseDetail){

        wmsPurchaseDetail.setStatus(WareConstant.PurchaseDetailStatusEnum.CREATED.getCode());
        boolean save = wmsPurchaseDetailService.save(wmsPurchaseDetail);

        return  R.ok(save);
    }

    @PostMapping("/update")
    public R<Boolean> update(@RequestBody WmsPurchaseDetail wmsPurchaseDetail){

        boolean update = wmsPurchaseDetailService.updateById(wmsPurchaseDetail);

        return  R.ok(update);
    }

    @GetMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable Integer id){

        boolean remove = wmsPurchaseDetailService.removeById(id);

        return  R.ok(remove);
    }
}

